// var BASE_URL = "https://xiangpijiemi.com/api/";
var BASE_URL = "https://lifeborn.admin.pro.sdyunshengkeji.com/api/";
const globletoken = 'KOxNSkiwYyZPJduPaR9Yl8FpTP1Qj4QcA888'
function post(api, Data, token,next) {
    uni.showLoading({
        title: '玩命请求中',
        mask: true
    })
    let header = token ? {
        "Content-Type": "application/x-www-form-urlencoded",
        token: uni.getStorageSync("token") || globletoken,
    } : { "Content-Type": "application/x-www-form-urlencoded" };
    // if(token){
    //     Data.token =  uni.getStorageSync("token") 
    // }

    return new Promise((resolve, reject) => {
        uni.request({
            url: BASE_URL + api,
            data: Data,
            method: "POST",
            header: header,
            success: (res) => {
                console.log(res)
                uni.hideLoading()
                if(res.statusCode == 200){
                    if (res.data.code == 1) {
                        console.log(res)
                        resolve(res.data);
                    }else{
                        if(next){
                            resolve(res.data);
                        }else{
                            if (res.data.code == 401) {
                                console.log(res.data.msg)
                                uni.showToast({
                                    icon: 'none',
                                    title: res.data.msg
                                })
                                
                                setTimeout(() => {
                                    return uni.reLaunch({ url: '/pages/login/index' })
                                }, 1000);
                            }
                            if (res.data.code == -1) {
                                console.log(res.data.msg)
                                uni.showToast({
                                    icon: 'none',
                                    title: res.data.msg
                                })
                                
                                setTimeout(() => {
                                    return uni.reLaunch({ url: '/pages/login/index' })
                                }, 1000);
                            } else {
                                
                                uni.showToast({
                                    icon: 'none',
                                    title: res.data.msg
                                }) 
                            }
                        }
                        
                    }
                }else{
                    uni.showToast({
                        icon: 'none',
                        title: '网络错误'
                    }) 
                }
               
                

            },
            fail: (err) => {
                uni.showToast({
                    icon: "none",
                    title: "请求接口失败",
                });
                reject(err);
            },
        });
    });
}

function get(api, Data, token,next) {

    let header = token ? {
        "Content-Type": "application/x-www-form-urlencoded",
        token: uni.getStorageSync("token") || globletoken,
    } : { "Content-Type": "application/x-www-form-urlencoded" };
    return new Promise((resolve, reject) => {

        uni.request({
            url: BASE_URL + api, //仅为示例，并非真实接口地址。
            data: Data,
            method: "GET",
            header: header,
            success: (res) => {
                if(res.statusCode == 200){
                    if (res.data.code == 1) {
                   
                        resolve(res.data);
                    }else{

                        if(next){
                            resolve(res.data);
                        }else{

             
                        if (res.data.code == -1) {
                            console.log(res.data.msg)
                            uni.showToast({
                                icon: 'none',
                                title: res.data.msg
                            })
                            
                            setTimeout(() => {
                                return uni.reLaunch({ url: '/pages/login/index' })
                            }, 1000);
                        } else {
                            uni.showToast({
                                icon: 'none',
                                title: res.data.msg
                            }) 
                        }
                    }
                    }
                }else{
                    uni.showToast({
                        icon: 'none',
                        title: '网络错误'
                    }) 
                }
            },
            fail: (err) => {
                uni.showToast({
                    icon: "none",
                    title: "请求接口失败",
                });
                console.log(err);
                reject(err);
            },
        });
    });
}

function upload(api, filePath) {

    return new Promise((resolve, reject) => {
        uni.uploadFile({
            url: BASE_URL + api,
            filePath: filePath,
            name: 'file',

            formData: {
                // token: uni.getStorageSync("token") || globletoken,
            },
            success: (res) => {
                console.log(res)
                if(res.statusCode == 200){
                    res.data = JSON.parse(res.data)
                    if (res.data.code == 1) {
       
                        resolve(res.data);
                    }else{
                        if (res.data.code == -1) {
                            console.log(res.data.msg)
                            uni.showToast({
                                icon: 'none',
                                title: res.data.msg
                            })
                            
                            setTimeout(() => {
                                return uni.reLaunch({ url: '/pages/login/index' })
                            }, 1000);
                        } else {
                            uni.showToast({
                                icon: 'none',
                                title: res.data.msg
                            }) 
                        }
                    }
                }else{
                    uni.showToast({
                        icon: 'none',
                        title: '网络错误'
                    }) 
                }
            },
            fail: (err) => {
                uni.showToast({
                    icon: "none",
                    title: "请求接口失败",
                });
                console.log(err);
                reject(err);
            },
            complete:(com)=>{
                uni.hideToast()
            }
        });
    })
}
export default {
	post: post,
	get: get,
	upload: upload,
};
